package com.hand.qt24953.ssmtrain.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import com.hand.qt24953.ssmtrain.entity.Item;
import com.hand.qt24953.ssmtrain.service.ItemService;
import com.hand.qt24953.ssmtrain.util.ExportExcelUtils;
import com.hand.qt24953.ssmtrain.util.Payload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author qiutao
 * @date 2019/7/19
 */
@RestController
public class ItemController {
    @Autowired()
    private ItemService itemService;

    @GetMapping("/excel/item")
    public void ExportItem(HttpServletRequest request, HttpServletResponse response){
        List<Item> itemList = itemService.findPage(new Item(), null);
        String excelName = "物料表";
        LinkedHashMap<String, String> fieldMap = new LinkedHashMap<>();
        fieldMap.put("itemCode", "物料编码");
        fieldMap.put("itemDescription", "物料描述");
        fieldMap.put("itemUom", "物料单位");
        fieldMap.put("startActiveDate", "生效日期从");
        fieldMap.put("endActiveDate", "生效日期至");
        fieldMap.put("enabledFlag", "是否启用");

        ExportExcelUtils.export(excelName, itemList, fieldMap, response);
    }

    @GetMapping("/item")
    public Payload listItem(@RequestParam(value = "itemDescription", required = false) String itemDescription,
                            @RequestParam(value = "itemUom", required = false) String itemUom,
                            @RequestParam(value = "startActiveDate", required = false) String startActiveDate,
                            @RequestParam(value = "endActiveDate", required = false) String endActiveDate,
                            @RequestParam(value = "itemCode", required = false) String itemCode,
                            @RequestParam(value = "enabledFlag", required = false) Boolean enabledFlag,
                            @RequestParam(value = "order", required = false) String order,
                            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                            @RequestParam(value = "size", defaultValue = "10", required = false) Integer size){
        PageHelper.startPage(page,size);
        Item item = new Item();
        item.setItemDescription(itemDescription);
        item.setItemUom(itemUom);
        if (StringUtil.isNotEmpty(startActiveDate)){
            DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate startDate = LocalDate.parse(startActiveDate, pattern);
            item.setStartActiveDate(localDate2Date(startDate));
        }
        if (StringUtil.isNotEmpty(endActiveDate)){
            DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            LocalDate endDate = LocalDate.parse(endActiveDate, pattern);
            item.setEndActiveDate(localDate2Date(endDate));
        }
        item.setItemCode(itemCode);
        item.setEnabledFlag(enabledFlag);
        System.out.println(item);
        PageInfo pageInfo = new PageInfo(itemService.findPage(item, order));
        return new Payload(pageInfo);
    }

    public static Date localDate2Date(LocalDate localDate) {
        if(null == localDate) {
            return null;
        }
        ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
        return Date.from(zonedDateTime.toInstant());
    }

    @GetMapping("/item/{id}")
    public Payload getItem(@PathVariable("id") Long id){
        return new Payload(itemService.detail(id));
    }

    @PostMapping("/item")
    public Payload create(@RequestBody @Valid Item item){
        return new Payload(itemService.create(item));
    }

    @PutMapping("/item/{id}")
    public Payload update(@PathVariable("id")Long id, @RequestBody Item item){
        return new Payload(itemService.update(id, item));
    }

    @DeleteMapping("/item/{id}")
    public Payload delete(@PathVariable("id")Long id){
        return new Payload(itemService.delete(id));
    }

    @DeleteMapping("/item")
    public Payload delete(@RequestBody Long[] ids){
        return new Payload(itemService.delete(ids));
    }
}
